package edu.gmu.projects.fireresponder.gui;

import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import edu.gmu.projects.fireresponder.R;
import edu.gmu.projects.fireresponder.missionplanner.SituationEnvironment;

public class SituationMapPreferences extends PreferenceActivity {
	public static final String MISSION_PLAN_PREF = "mPlanPref";
	public static final String CLEAR_PLAN_PREF = "clearPlanPref";
	public static final String RESPONDER_NAME_PREF = "frNamePref";
	public static final String USE_TTS_PREF = "useTtsPref";
	public static final String USE_VIB_PREF = "useVibPref";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.situation_map_prefrences);
		Preference missionPlanPref = (Preference) findPreference(MISSION_PLAN_PREF);
		missionPlanPref
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						String missionPlanValue = (String) newValue;
						Intent intent = new Intent(
								SituationMapPreferences.this,
								SituationMap.class);

						intent.putExtra(SituationMap.MISSION_PLAN_VALUE,
								missionPlanValue);
						startActivity(intent);

						return true;
					}
				});
		Preference clearPlanPref = (Preference) findPreference(CLEAR_PLAN_PREF);
		clearPlanPref
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference preference) {
						Intent intent = new Intent(
								SituationMapPreferences.this,
								SituationMap.class);

						intent.putExtra(SituationMap.CLEAR_MISSION_PLAN,
								"Clear");
						startActivity(intent);

						return true;
					}
				});
		Preference frNamePref = (Preference) findPreference(RESPONDER_NAME_PREF);
		String name = frNamePref.getSharedPreferences().getString(
				frNamePref.getKey(), "Anonymous");

		frNamePref.setSummary(name);
		frNamePref
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {

						SituationEnvironment.getSituationEnvironment(
								SituationMapPreferences.this).getSelf().setId(
								(String) newValue);

						preference.setSummary((String) newValue);
						return true;
					}
				});

		CheckBoxPreference useVibPref = (CheckBoxPreference) findPreference(USE_VIB_PREF);
		useVibPref.setSummary(useVibPref.isChecked() ? "Always" : "Never");
		useVibPref
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {

						boolean isChecked = (Boolean) newValue;

						preference.setSummary(isChecked ? "Always" : "Never");

						return true;
					}
				});
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.i("SituationMapPreference", "Started activity");
	}

	@Override
	public void onDestroy() {
		Log.i("SituationMapPreference", "Destroyed activity");
		super.onDestroy();
	}

	@Override
	public void onRestart() {
		super.onRestart();
		Log.i("SituationMapPreference", "Restarted activity");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.i("SituationMapPreference", "Resumed activity");
	}

	@Override
	public void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Log.i("SituationMapPreference", "New intent received for activity");
	}
}